
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutterlib/common/constants/constants.dart';
import 'package:flutterlib/common/datacenter/user_center.dart';
import 'package:flutterlib/common/net/dao_result.dart';
import 'package:flutterlib/common/net/retrofit/activity_api.dart';
import 'package:flutterlib/common/utils/distance_utils.dart';
import 'package:flutterlib/common/utils/navigator_utils.dart';
import 'package:flutterlib/model/activity/activity_page_new_query.dart';
import 'package:flutterlib/model/activity/activity_page_query.dart';
import 'package:flutterlib/pages/base/base_sliver_list_noappbar.dart';
import 'package:flutterlib/widgets/bussiness/activity/activity_list_item_new.dart';
import 'package:flutterlib/widgets/quhui_select_item_widget.dart';
import 'package:flutterlib/widgets/pull/nested/sliver_header_delegate.dart';

class ActivityNormalListWidgetPage extends BaseSliverListNoAppbar {
  final String name;
  final int interestId;
  final int type;

  ActivityNormalListWidgetPage({this.name, Key key, this.interestId, this.type})
      : super(key: key);

  @override
  ActivityNormalListWidgetPageState createState() =>
      ActivityNormalListWidgetPageState();
}

///页面 KeepAlive ，同时支持 动画Ticker
class ActivityNormalListWidgetPageState
    extends BaseSliverListNoAppbarState<ActivityNormalListWidgetPage> {
  final Map<int, dynamic> choiceSelectMap = new Map();
  final SettingsStore store =
  Provider.of<SettingsStore>(currentContext, listen: false);


  getDataLogic(int page) async {

    print(page);
    int isOnline = choiceSelectMap[0] != null ? (choiceSelectMap[0] - 1) : null;
    int isCompany = (choiceSelectMap[2] != null && choiceSelectMap[2] == 2) ? 1 : null;

    var activitys = await ActivityApi.builder().pageByNewQuery(
      ActivityPageQuery(
          interestId: widget.interestId,
          type: widget.type,
          userId: currentUserId,
          isOnline: isOnline,
          pageNum: page,
          isCompany: isCompany,
          orderField: getOrderFiledByChoice(choiceSelectMap[3]),
          ),

    );
    return new DataResult(activitys.list, true);
  }

  Widget renderItem(index, activity) {
    //提交
    // var activity = pullLoadWidgetControl.dataList[index];
    Poi point;
    if(store.myPosition != null){

      point = Poi(longitude: store.myPosition.longitude.toString(), latitude: store.myPosition.latitude.toString());
    }
    return ActivityListItemNew(activity, myLocation: point, onPressed: () {
      NavigatorUtils.gotoActivityDetail(context, activity.id);
    });
  }

  ///绘制内置Header，支持部分停靠支持
  List<Widget> renderHeader(BuildContext context, bool innerBoxIsScrolled) {
    return <Widget>[
      ///动态放大缩小的tab控件
      SliverPersistentHeader(
        pinned: true,

        /// SliverPersistentHeaderDelegate 的实现
        delegate: SliverHeaderDelegate(
            maxHeight: 60,
            minHeight: 60,
            changeSize: true,
            snapConfig: FloatingHeaderSnapConfiguration(
              vsync: this,
              curve: Curves.bounceInOut,
              duration: const Duration(milliseconds: 10),
            ),
            builder: (BuildContext context, double shrinkOffset,
                bool overlapsContent) {
              ///根据数值计算偏差
              var radius = Radius.circular(
                  0); //Radius.circular(4 - shrinkOffset / 60 * 4);
              Widget sizeBox = SizedBox.expand(
                child: Padding(
                  padding: EdgeInsets.only(bottom: 10, left: 0, right: 0),
                  child: new QuhuiSelectItemWidget(
                    (tabIndex, choiceIndex) {
                      ///切换时先滑动
                      choiceSelectMap[tabIndex] = choiceIndex;
                      manger.refresh();
//                            showRefreshLoading();
                    },
                    margin: EdgeInsets.zero,
                  ),
                ),
              );

              return sizeBox;
            }),
      ),
    ];
  }

  getOrderFiledByChoice(choiceSelectMap) {
    if(choiceSelectMap == null || choiceSelectMap == 0 || choiceSelectMap == 1) return null;
    if(choiceSelectMap == 2) return 'distance';
  }

  @override
  bool enablePullDown() {
    return true;
  }

  @override
  bool enablePullUp() {
    return true;
  }
}
